package com.dartfar.workflow.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.dartfar.base.action.BaseAction;
import com.dartfar.base.vo.DataGridVo;
import com.dartfar.workflow.model.WorkflowTaskFunMount;
import com.dartfar.workflow.service.WorkflowTaskFunMountService;
import com.dartfar.workflow.service.WorkflowTaskService;
import com.dartfar.workflow.vo.WorkflowButtonsVo;
import com.dartfar.workflow.vo.WorkflowCurUserTasksVo;
import com.dartfar.workflow.vo.WorkflowExecuteTaskVo;
import com.dartfar.workflow.vo.WorkflowTaskFunMountVo;


/**
 * 流程任务节点Action
 * @author 王琦
 *
 */
@Scope("prototype")
@Component("workflowTasksAction")
public class WorkflowTasksAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	static Logger logger = Logger.getLogger(WorkflowTasksAction.class);
	
	/**
	 * Spring 注入
	 */
	@Resource
	private WorkflowTaskFunMountService workflowTaskFunMountService;
	@Resource
	private WorkflowTaskService workflowTaskService; 
	
	private int id;
	private String updated;
	
	//任务ID
	private String taskId;
	//提交类型
	private String submitType;
	//留言信息
	private String lmContent;
	private String assigne;
	
	
		
	/**
	 * 保存任务节点信息
	 * @return
	 */
	public String save(){
		List<WorkflowTaskFunMountVo> list = null;
		//更新键值
		logger.info("updated: " + updated);
		if(updated != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(updated, WorkflowTaskFunMountVo.class);
			for(Iterator<WorkflowTaskFunMountVo> w =  list.iterator(); w.hasNext();){
				WorkflowTaskFunMountVo wtfVo = w.next();
				edit(wtfVo);
			}
		}
		logger.info("执行完毕");
		return SUCCESS;
	}
	
	/**
	 * 编辑
	 * @param wtfVo
	 */
	public void edit(WorkflowTaskFunMountVo wtfVo){
		WorkflowTaskFunMount wftfm = workflowTaskFunMountService.get(wtfVo.getId());
		wftfm.setAbleFileds(wtfVo.getAbleFileds());
		wftfm.setAbleButtons(wtfVo.getAbleButtons());
		workflowTaskFunMountService.update(wftfm);
	}
	
	
	
	/**
	 * 获取挂接流程下所有活动节点
	 * @return
	 */
	public String loadWorkflowTasksData(){
		DataGridVo<WorkflowTaskFunMountVo> dataGridVos = new DataGridVo<WorkflowTaskFunMountVo>();
		List<WorkflowTaskFunMountVo> workflowTaskFunMountVos = new ArrayList<WorkflowTaskFunMountVo>();
		
		//列表
		List<WorkflowTaskFunMount> list = workflowTaskFunMountService.getListForPageByWorkflowProcessId(id, pageNo, pageSize);
		//记录数
		Long count = workflowTaskFunMountService.getCountByWorkflowProcessId(id);
		for(Iterator<WorkflowTaskFunMount> w =  list.iterator(); w.hasNext();){
			WorkflowTaskFunMount wtf = w.next();
			WorkflowTaskFunMountVo wtfVo = new WorkflowTaskFunMountVo();
			wtfVo.setId(wtf.getId());
			wtfVo.setProcessDefinitionId(wtf.getProcessDefinitionId());
			wtfVo.setTaskNodeId(wtf.getTaskNodeId());
			wtfVo.setTaskNodeName(wtf.getTaskNodeName());
			wtfVo.setAbleFileds(wtf.getAbleFileds());
			wtfVo.setAbleButtons(wtf.getAbleButtons());
			workflowTaskFunMountVos.add(wtfVo);
		}
	
		//设置VO
		dataGridVos.setTotal(count);
		dataGridVos.setRows(workflowTaskFunMountVos);
		logger.info("count: " + count);
		String jsonResult = JSONObject.fromObject(dataGridVos).toString();
		logger.info(jsonResult);
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}	
		return null;
	}

	/**
	 * 得到当前登录人所有的任务节点
	 * @return
	 */
	public String loadCurUserTasksData(){
		logger.info("PageNo: " + pageNo);
		logger.info("pageSize: " + pageSize);
		logger.info("assigne: " + assigne);
		try{
			List<WorkflowCurUserTasksVo> tasks = workflowTaskService.getCurrentUserTask(curPerson(), pageNo, pageSize);
			logger.info("tasks: " + tasks.size());
			String jsonResult = JSONArray.fromObject(tasks).toString();
			response.getWriter().write(jsonResult);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 执行任务
	 * @return
	 */
	public String executeTask(){
		logger.info("taskId: " + taskId);
		logger.info("modelName: " + modelName);
		logger.info("id: " + id);
		logger.info("assignees: " + assignees);
		logger.info("assigneType: " + assigneType);
		logger.info("assigneRoles: " + assigneRoles);
		logger.info("lmContent: " + lmContent);
		try{
			JSONObject returnObj = new JSONObject();
			WorkflowExecuteTaskVo wetVo = new WorkflowExecuteTaskVo();
			wetVo.setSubmitType(submitType);
			wetVo.setTaskId(taskId);
			wetVo.setModelName(modelName);
			wetVo.setModelId(String.valueOf(id));
			wetVo.setAssignees(assignees);
			wetVo.setAssigneType(assigneType);
			wetVo.setRoles(assigneRoles);
			wetVo.setComments(lmContent);
			//执行任务
			workflowTaskService.execTasks(curPerson(), wetVo, returnObj);
			logger.info("returnObj: " + returnObj);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
		//wfTaskManager.doSubmit(wfTaskInfoVo, returnObj);
		
		return null;
	}
	
	public void execTakeTask(){
		JSONObject returnObj = new JSONObject();
		try {
			workflowTaskService.execTakeTask(curPerson(), taskId, returnObj);
			response.getWriter().write(returnObj.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 执行撤销操作
	 * @return
	 */
	public String execRepealTask(){
		try{
			workflowTaskService.execRepealTask(taskId, modelName, String.valueOf(id));
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取按钮信息
	 */
	public String loadWorkflowButtons(){
		List<WorkflowButtonsVo> workflowButtonsVos = new ArrayList<WorkflowButtonsVo>();
		WorkflowButtonsVo wbv1 = new WorkflowButtonsVo();
		wbv1.setId("WF_START");
		wbv1.setName("WF_START");
		workflowButtonsVos.add(wbv1);
		
		WorkflowButtonsVo wbv2 = new WorkflowButtonsVo();
		wbv2.setId("WF_SEND");
		wbv2.setName("WF_SEND");
		workflowButtonsVos.add(wbv2);
		
		WorkflowButtonsVo wbv3 = new WorkflowButtonsVo();
		wbv3.setId("WF_BACK");
		wbv3.setName("WF_BACK");
		workflowButtonsVos.add(wbv3);
		
		WorkflowButtonsVo wbv4 = new WorkflowButtonsVo();
		wbv4.setId("WF_RETURN");
		wbv4.setName("WF_RETURN");
		workflowButtonsVos.add(wbv4);
		
		WorkflowButtonsVo wbv5 = new WorkflowButtonsVo();
		wbv5.setId("WF_TASK");
		wbv5.setName("WF_TASK");
		workflowButtonsVos.add(wbv5);
		
		WorkflowButtonsVo wbv6 = new WorkflowButtonsVo();
		wbv6.setId("WF_REPEAL");
		wbv6.setName("WF_REPEAL");
		workflowButtonsVos.add(wbv6);
		
		logger.info("workflowButtonsVos: " + workflowButtonsVos.size());
		String jsonResult = JSONArray.fromObject(workflowButtonsVos).toString();
		logger.info(jsonResult);
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}	
		
		return null;
	}
	
	/**
	 * Getter And Setter
	 */
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUpdated() {
		return updated;
	}

	public void setUpdated(String updated) {
		this.updated = updated;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getLmContent() {
		return lmContent;
	}

	public void setLmContent(String lmContent) {
		this.lmContent = lmContent;
	}

	public String getSubmitType() {
		return submitType;
	}

	public void setSubmitType(String submitType) {
		this.submitType = submitType;
	}

	public String getAssigne() {
		return assigne;
	}

	public void setAssigne(String assigne) {
		this.assigne = assigne;
	}
	

}
